package com.gzw.dms.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gzw.dms.MyApplication;
import com.gzw.dms.R;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.bean.AbnormalReportInfo;
import com.gzw.dms.bean.ShippingOrderInfo;
import com.gzw.dms.bean.UserInfo;
import com.gzw.dms.bean.WaybillAdjustmentInfo;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderAuditingDetailActivity extends BaseActivity {
    private TextView tv_shiping_order_num, tv_custom_name, tv_custom_contact,
            tv_custom_tel, tv_receipt_tel, tv_goods_name, tv_send_station,tv_send_mechanism
            ,tv_end_mechanism,tv_shiping_order_goid,auditing_reason,auditing_type;
    private EditText ed_result;
    private Button btn_edit;
    private Intent mIntent;
    private WaybillAdjustmentInfo waybillAdjustmentInfo;
    private ProgressDialog mProgressDialog;
    private Context mContext;
    private Toolbar toolbar;
    //0、1为接单，2已接单，3电子围栏签收，4实际签收,5分配订单
    private  String state;
    private List<String> ls_map;
    private String auditor_message = "";
    private LinearLayout layout_sh,layout_audit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_order_auditing_detail);

    }

    @Override
    protected void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        tv_shiping_order_num = (TextView) findViewById(R.id.ic_shiping_order_num).findViewById(R.id.tv_name);
        tv_shiping_order_goid = (TextView) findViewById(R.id.ic_shiping_order_goid).findViewById(R.id.tv_name);
        tv_send_station = (TextView) findViewById(R.id.ic_send_station).findViewById(R.id.tv_name);
        tv_send_mechanism = (TextView) findViewById(R.id.ic_send_mechanism).findViewById(R.id.tv_name);

//        tv_end_address = (TextView) findViewById(R.id.ic_end_address).findViewById(R.id.tv_name);
        tv_custom_name = (TextView) findViewById(R.id.ic_custom_name).findViewById(R.id.tv_left);
        auditing_reason = (TextView) findViewById(R.id.ic_custom_name).findViewById(R.id.tv_right);
        tv_custom_contact = (TextView) findViewById(R.id.ic_custom_contact).findViewById(R.id.tv_name);
        tv_custom_tel = (TextView) findViewById(R.id.ic_custom_tel).findViewById(R.id.tv_name);

        tv_receipt_tel = (TextView) findViewById(R.id.ic_receipt_tel).findViewById(R.id.tv_right);
        auditing_type = (TextView) findViewById(R.id.ic_receipt_tel).findViewById(R.id.tv_left);
        tv_end_mechanism = (TextView) findViewById(R.id.ic_end_mechanism).findViewById(R.id.tv_name);
        tv_goods_name = (TextView) findViewById(R.id.ic_goods).findViewById(R.id.tv_name);
        layout_sh = (LinearLayout) findViewById(R.id.layout_sh);
        layout_sh.setVisibility(View.GONE);
        layout_audit = (LinearLayout) findViewById(R.id.layout_audit);
        btn_edit = (Button) findViewById(R.id.btn_edit);
        ed_result = ((EditText) findViewById(R.id.ed_result));


    }

    @Override
    protected void initData() {
        mContext = this;
        toolbar.setTitle("运单审核");
        mIntent = getIntent();
        ls_map = new ArrayList<>();
        waybillAdjustmentInfo = (WaybillAdjustmentInfo) mIntent.getExtras().get(
                "waybillAdjustmentInfo");

        setData(waybillAdjustmentInfo);

        String flag = MyApplication.getUserInfo().getFlag();
        if (flag.equals("0")&&waybillAdjustmentInfo.getApplicant_state()==0){
            layout_sh.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initEvents() {
        btn_edit.setOnClickListener(new mOnClick());
        toolbar.setNavigationOnClickListener(new mOnClick());
    }

    private void setData(WaybillAdjustmentInfo waybillAdjustmentInfo) {
        ShippingOrderInfo shippingOrderInfo = waybillAdjustmentInfo.getShippingOrder();
        tv_shiping_order_num.setText("货运编号:" + "    " + shippingOrderInfo.getShiping_order_num());
        tv_shiping_order_goid.setText("出货订单号:" + "    " + shippingOrderInfo.getShiping_order_goid());
        tv_send_mechanism.setText("受理机构:" + "    " + shippingOrderInfo.getSend_mechanism());
        tv_send_station.setText("终到位置:" + "    " + shippingOrderInfo.getEnd_address());

//        tv_end_address.setText("终到位置:" + "    " + shippingOrderInfo.getEnd_address());
        tv_custom_name.setText("申请人姓名:" + "    " + waybillAdjustmentInfo.getApplicant_name());
        tv_custom_contact.setText("申请时间:" + "    " + waybillAdjustmentInfo.getApplicant_time());
        tv_custom_tel.setText("申请原因:" + "    " + waybillAdjustmentInfo.getApplicant_message());
        if (waybillAdjustmentInfo.getApplicant_type() == 0) {
            auditing_reason.setText("申请类型: 运单调整");
        } else if (waybillAdjustmentInfo.getApplicant_type() == 1) {
            auditing_reason.setText("申请类型: 取消签收");
        }
        if (waybillAdjustmentInfo.getApplicant_state() == 0) {
            auditing_type.setText("审核状态: 未审核");
            layout_audit.setVisibility(View.GONE);
        } else if (waybillAdjustmentInfo.getApplicant_state() == 1) {
            auditing_type.setText("审核状态: 已审核");
            tv_receipt_tel.setText("审核人姓名:" + "    " + waybillAdjustmentInfo.getAuditor_name());
            tv_end_mechanism.setText("审核时间:" + "    " + waybillAdjustmentInfo.getAuditor_time());
            tv_goods_name.setText("审核意见:" + "    " + waybillAdjustmentInfo.getAuditor_message());
        }



    }


    class mOnClick implements View.OnClickListener {


        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                case R.id.btn_edit:
                    if (isNull(ed_result)) {
                        T.showShort(mContext, "处理信息不能为空");
                        return;
                    }
                    auditor_message = ed_result.getText().toString().trim();
                    ShowOrderTask signShipOrder = new ShowOrderTask();
                    signShipOrder.execute();
                    break;

                default:
                    Intent mIntent = new Intent();
                    //把返回数据存入Intent
                    mIntent.putExtra("result", 1);
                    //设置返回数据
                    setResult(1, mIntent);
                    finish();
                    break;
            }
        }
    }


    public class ShowOrderTask extends AsyncTask<String, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {

            mProgressDialog = ProgressDialog.show(mContext, null, "正在加载中...");
            mProgressDialog.setCancelable(true);
            mProgressDialog.setCanceledOnTouchOutside(false);

        }
        protected String doInBackground(String... params) {
            Map<String, Object> map = new HashMap<>();
            UserInfo userInfo = MyApplication.getUserInfo();
            map.put("userInfo", userInfo);
            map.put("waybillAdjustmentId", waybillAdjustmentInfo.getId());
            map.put("auditor_message", auditor_message);
            map.put("shiping_order_id", waybillAdjustmentInfo.getShippingOrder().getShiping_order_id());
            map.put("applicant_type", waybillAdjustmentInfo.getApplicant_type());
            map.put("shiping_order_num", waybillAdjustmentInfo.getShippingOrder().getShiping_order_num());
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appDoWaybillAuditInfo"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {
            mProgressDialog.dismiss();
            if (isNull(result)) {
                T.showShort(mContext, "连接服务器异常");
            } else {
                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                } else {
                    String successMsg = JsonUtil.getMsg(result, "successMsg");
                    T.showShort(mContext, successMsg);
                    Intent mIntent = new Intent();
                    //把返回数据存入Intent
                    mIntent.putExtra("result", 1);
                    //设置返回数据
                    setResult(1, mIntent);
                    finish();
                }
            }
        }
    }


}
